package com.ironman.sailboat.mobile.runner;

import cn.hutool.core.util.StrUtil;
import com.ironman.sailboat.mobile.computer.v2.TrainComputer;
import com.ironman.sailboat.mobile.context.MockConfigContext;
import com.ironman.sailboat.mobile.dataobject.td.Wind;
import com.ironman.sailboat.mobile.threadpool.ThreadPool;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.row.RowUtil;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.noear.snack.ONode;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * description 传感器runner
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2023/12/15 9:48
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class EnvMockRunner extends AbstractMockRunner implements ApplicationListener<ApplicationReadyEvent> {

    final TrainComputer trainComputer;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("WindOriginalRunner init start");
        ThreadPool.FIXED_EXECUTOR.submit(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                String sql = "SELECT ts, fill_type, wind_direction, wind_speed FROM `sailboat`.`wind` WHERE tenant_code = ? AND training_boat_code = ? AND ts >=? AND ts <= ?";
                List<Row> rows = Db.selectListBySql(sql, MockConfigContext.fromTenantCode(), MockConfigContext.fromTrainingBoatCode(), getSysTrainingProperties().getStartTime(), getSysTrainingProperties().getEndTime());
                Map<Date, Wind> dateMap = rows.stream().collect(Collectors.toMap(n -> n.getDate("ts"), n -> RowUtil.toEntity(n, Wind.class)));
                log.info("Env rows: {}", rows.size());
                setStartAndEndTime(rows);
                COUNT_DOWN_LATCH.countDown();
                COUNT_DOWN_LATCH.await();
                traceBackData(date -> {
                    Wind wind = dateMap.get(date);
                    if (Objects.isNull(wind)) {
                        return null;
                    }
                    if (StrUtil.isBlank(wind.getFillType())) {
                        return null;
                    }
                    boolean fillType = "1".equals(wind.getFillType());
                    if (fillType) {
                        return null;
                    }
                    trainComputer.wind(MockConfigContext.fromTrainingBoatCode(),
                            wind.getWindSpeed() * 1.0, wind.getWindDirection(),
                            514.44, 100);
                    return ONode.newObject().set("dataType", "wind").set("data", MockConfigContext.fromTrainingBoatCode());
                });
            }
        });
        log.info("WindOriginalRunner init finish");
    }
}
